package com.lmh.backend.dao.daoImpl;

import com.lmh.backend.dao.AuthorDao;
import com.lmh.backend.entity.Author;
import com.lmh.backend.entity.News;
import com.lmh.backend.entity.User;
import com.lmh.backend.utils.JpaUtils;
import org.springframework.stereotype.Repository;

import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
import javax.persistence.Query;
import java.util.List;
import java.util.Set;

@Repository
public class AuthorDaoImpl implements AuthorDao {
    @Override
    public Author getAuthorByName(String authorName) {
        EntityManager manager = JpaUtils.getEntityManager();
        Query query = manager.createQuery("select a from Author a where a.username = ?");
        query.setParameter(1, authorName);
        List<Author> resultList = query.getResultList();
        return resultList.isEmpty()?null:resultList.get(0);
    }

    @Override
    public void save(Author author) {
        EntityManager manager = JpaUtils.getEntityManager();
        EntityTransaction transaction = manager.getTransaction();
        transaction.begin();
        manager.persist(author);
        transaction.commit();
    }
}
